﻿Public Class dbaseProc

    Public WithEvents theHTML As System.Windows.Forms.HtmlDocument
    Public Event C_pageRightClicked(ByVal sender As Object, ByVal e As Windows.Forms.HtmlElementEventArgs)
    Public Event C_pageClicked(ByVal sender As Object, ByVal e As Windows.Forms.HtmlElementEventArgs)

    Public Sub New(ByVal sqlstr As String)

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        Browser.Navigate("about:blank")

        theHTML = Browser.Document
        AddHandler theHTML.ContextMenuShowing, AddressOf defaultCMSshow
        AddHandler theHTML.Click, AddressOf Me.document_onclick


        loadPage(getinfofromDB(sqlstr))


    End Sub








    Public Sub loadPage(ByVal html As String)
        theHTML.OpenNew(True)
        theHTML.Write(html)
    End Sub

    Private Sub defaultCMSshow(ByVal sender As Object, ByVal e As Windows.Forms.HtmlElementEventArgs)
        e.ReturnValue = False
        RaiseEvent C_pageRightClicked(sender, e)
    End Sub

    Private Sub document_onclick(ByVal sender As Object, ByVal e As Windows.Forms.HtmlElementEventArgs)
        e.ReturnValue = False
        RaiseEvent C_pageClicked(sender, e)
    End Sub



    Private Function theHTML_Click(ByVal sender As Object, ByVal e As System.Windows.Forms.HtmlElementEventArgs) As Boolean Handles theHTML.Click

        Try
            Dim element As System.Windows.Forms.HtmlElement = theHTML.GetElementFromPoint(e.ClientMousePosition)

            Dim splitArray As String()
            splitArray = Split(element.Id, ",")

            Dim linkType As String = splitArray(0)
            Dim elementID As String = Nothing
            If UBound(splitArray) > 0 Then elementID = splitArray(1)

            Select Case linkType


                Case "l" 'a link
                    Dim href As String = element.GetAttribute("HREF")
                    System.Diagnostics.Process.Start(href)

            End Select

            e.ReturnValue = False
            Return False
        Catch ex As System.Exception
            XBRL.onerror(ex)
            e.ReturnValue = False
            Return False
        End Try


    End Function







   

    
   
    Dim sqlerror As String

    Public Function getinfofromDB(ByVal SQstr As String) As String

        Dim rowLength, colLength As Integer
        Dim nameArray()
        Dim typeArray()

        Dim resultLizt As DataTable

        Dim ee As New System.Text.StringBuilder
        ee.AppendLine(SQstr)
        ee.AppendLine("<br>")

        Try



            resultLizt = XBRL.fillFromDB(SQstr)
            'Get Table Info

            If Not (resultLizt Is Nothing) Then
                rowLength = resultLizt.Rows.Count
                colLength = resultLizt.Columns.Count


                ReDim nameArray(colLength)

                ReDim typeArray(colLength)

                Dim i As Integer = 0
                'http://msdn.microsoft.com/en-us/library/system.data.datacolumn_members(v=VS.100).aspx
                For Each mycolumn As DataColumn In resultLizt.Columns   'objDS.Tables("Info").Columns
                    nameArray(i) = mycolumn.ColumnName
                    typeArray(i) = mycolumn.DataType.ToString
                    i = i + 1
                Next
            End If

        Catch ex As Exception
            sqlerror = ex.ToString()
            ee.AppendLine("<br><b>Database Errors Occured</b><br>")
            ee.AppendLine(sqlerror)

            Return ee.ToString
        End Try

        Try



            ee.AppendLine("<br><table border = 1><tr bgcolor = '#aaaaaa'>")
            Dim c As Integer
            For c = 0 To colLength - 1
                ee.AppendLine("<td valign = top>")
                ee.AppendLine(nameArray(c) & "<br>")
                ee.AppendLine(typeArray(c))
                ee.AppendLine("</td>")
            Next
            ee.AppendLine("</tr>")
            Dim r As Integer
            For r = 0 To rowLength - 1
                ee.AppendLine("<tr>")
                For c = 0 To colLength - 1
                    ee.AppendLine("<td valign = top>")

                    If Len(resultLizt.Rows(r)(c).ToString) <> 0 Then

                        '        If typeArray(c) = 201 Then
                        '       if instr(0,resultLizt(ya,yo),"<html>") then

                        '    resultLizt.Rows(r)(c).ToString = htmlparse(resultLizt(ya, yo))
                        '      end if
                        'End If



                        ee.AppendLine(resultLizt.Rows(r)(c).ToString)

                    Else
                        ee.AppendLine("&nbsp;")
                    End If

                    ee.AppendLine("</td>")

                Next
                ee.AppendLine("</tr>")

            Next

            ee.AppendLine("</table>")

            ee.AppendLine("<br><br><br>")
            Return ee.ToString
        Catch ex As Exception
            XBRL.onerror(ex)

            Return ""
        End Try


    End Function

End Class